<?php

require_once '../config/controller.php';
require_once '../views/ViewAdminController.php';
require_once '../config/class.mysql.php';
require_once '../config/metadata.php';
require_once '../inc/simpleimage.php';

class FotosController extends Controller {

	protected static $baseUrl = '/panel/fotos.php';

	public function index_action() {
		$metadata = array('title' => "Gestión fotos");

		$categorias = $this->Mysql->getAll('categorias', 'id, nombre');
		$fotos = $this->Mysql->getJoin(array('fotos','categorias'), 'categorias.nombre, categorias.orden, fotos . *', array('fotos.id_categoria', 'categorias.id'),  'LEFT', '2 ASC,', '3 DESC');
		$data = array(
			'title' => "Gestión fotos",
			'categorias' => $categorias,
			'fotos' => $fotos,
			'baseUrl' => self::$baseUrl
		);

		$this->viewController->render('fotos', $data, $metadata);
	}

	public function insertar_action() {
		if(isset($_FILES['image']) && isset($_POST['categoria']) ){

			for ($i = 0; $i < count($_FILES['image']['tmp_name']); $i++) {
	
				$name = $this->Mysql->getLast('fotos','id, enlace');
				$rest = substr($name['enlace'], -4);
				$rest++;
				$file_name = 'foto'.str_pad($rest, 4, 0, STR_PAD_LEFT);


				$image = new SimpleImage();
			      	$image->load($_FILES['image']['tmp_name'][$i]);
				if($image->getWidth() > $image->getHeight()){
					$image->resizeToWidth(800);
				} else {
					$image->resizeToHeight(500);
				}
	
			      	$image->save('../fotos/'.$file_name.'.jpg');

				$image->resize(128,128);
				$image->save('../fotos/'.$file_name.'.mini.jpg');
	
				$this->Mysql->insert('fotos', array($file_name, $_POST['comentario'], $_POST['categoria']));
			}
		}
		$this->redirect();
	}

	public function borrar_action() {
		if(isset($_GET['id']) && isset($_GET['enlace']) ){
			$this->Mysql->delete('fotos', array('id' => $_GET['id']));
			unlink("../fotos/".$_GET['enlace'].".jpg");
			unlink("../fotos/".$_GET['enlace'].".mini.jpg");
		}
		$this->redirect();
	}

	public function editar_action() {
		$data = $metadata = array('title' => "Editar foto");

		if (!isset($_GET["enlace"])) die("Foto no especificada");
		else $data['file'] = $_GET["enlace"]. '.jpg';
		$data['foto'] = $this->Mysql->get('fotos', 'id', $_GET['id']);
		$data['categorias'] = $this->Mysql->getAll('categorias', 'id, nombre');
		$data['baseUrl'] = self::$baseUrl;

		if (isset($_POST['h']) && isset($_POST['w']) && isset($_POST['comentario']) && isset($_POST['categoria'])){

			if($_POST['h'] != "" && $_POST['w'] != "" && $_POST['x1'] != "" && $_POST['y1'] != "") {
				//print_r($_POST);
				$targ_w = $targ_h = 128;
				$jpeg_quality = 90;

				$src = '../fotos/' . $data['file'];
				$img_r = imagecreatefromjpeg($src);
				$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );

				imagecopyresampled($dst_r,$img_r,0,0,$_POST['x1'],$_POST['y1'],
				    $targ_w,$targ_h,$_POST['w'],$_POST['h']);
				@unlink('../fotos/' . $_GET["enlace"] .'.mini.jpg');
				imagejpeg($dst_r, '../fotos/' . $_GET["enlace"] .'.mini.jpg', $jpeg_quality);
		

			}
			$this->Mysql->update('fotos', array('comentario' => $_POST['comentario'], 'id_categoria' => $_POST['categoria']), "id='".$_GET['id']."'");
			echo '<script> parent.location.reload()</script>';
		}
		$this->viewController->setFull(false);
		$this->viewController->render('editarFoto', $data, $metadata);
	}

}

$controller = new FotosController('ViewAdminController', true);
$controller->dispatch();
